/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gblend.largegraph.graphdivide;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author HO HOANG HUNG
 */
public class BorderGraph {

    HashMap<Integer, Vector<Integer>> adjlist = new HashMap<Integer, Vector<Integer>>();
    HashMap<Integer,Integer> degreelist = new HashMap<Integer,Integer>();
    int id;

    public BorderGraph(int id) {
        this.id = id;
    }

    public void setGraphId(int nId) {
        id = nId;
    }

    public int getGraphId() {
        return id;
    }

    public void setDegree(int nodId,int degree){
        degreelist.put(nodId, degree);
    }

    public void addEdge(int fromId, int toId) {
        Vector<Integer> adjarray = adjlist.get(fromId);
        if (adjarray == null) {
            Vector<Integer> adjarrayN = new Vector<Integer>();
            adjarrayN.add(toId);
            adjlist.put(fromId, adjarrayN);
        } else {
            adjarray.add(toId);
        }
    }

    public void printToFile(String path) {
      //  System.out.println("path = "+path);
        BufferedOutputStream bos1 = null;
        try {
            File fl = new File(path + "/" + id);
            bos1 = new BufferedOutputStream(new FileOutputStream(fl));
            PrintStream q1 = new PrintStream(bos1);
            int sz = adjlist.size();
            q1.println("b "+sz);
            Set<Integer> adjCol = adjlist.keySet();
            Vector<Integer> adjColv = new Vector<Integer>(adjCol);
            Collections.sort(adjColv);
            Iterator<Integer> idIt = adjColv.iterator();
            Vector<Integer> adjArray = null;
            int vId = 0;
            StringBuffer ln;
            while (idIt.hasNext()) {
                vId = idIt.next();
                ln = new StringBuffer(vId + " ");
                adjArray = adjlist.get(vId);
                for (Enumeration<Integer> e = adjArray.elements(); e.hasMoreElements();) {
                    int adjId = e.nextElement();
                    ln.append(adjId+" ");
                }
                q1.println(ln.toString()+degreelist.get(vId));
            }
            q1.flush();
            bos1.close();
            q1.close();            
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                bos1.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
